#include "Terrain.h"


Terrain::Terrain(void)
{
}


Terrain::~Terrain(void)
{
}

bool SortXValues(CL_Vec3f a, CL_Vec3f b)
{
	return a.x < b.x;
}

vector<CL_Vec3f> Terrain::Initialize()
{
	srand(time(0));
	//int randomPoints = rand() % 100 + 10;
	int randomPoints = SCREENWIDTH / 20;
	CL_Vec3f start(0.0f, 400.0f, 0.0f);
	CL_Vec3f end(800.0f, 400.0f, 0.0f);
	vector<CL_Vec3f> positions;
	float prevY;

	m_points.push_back(start);
	for(int i = 0; i < randomPoints; i++)
	{
		if(i == 4 || i == 26)
		{
			if(i==4)
			{
				positions.push_back(CL_Vec3f(start.x + 20*i, prevY-10, 0.0f));
			}
			if(i==26)
			{
				positions.push_back(CL_Vec3f(start.x + 20*i, prevY-10, 0.0f));
			}
			m_points.push_back(CL_Vec3f(start.x + 20*(i+1), prevY, 0.0f));
		}
		else
		{
			prevY = GetRandomYValue();
			m_points.push_back(CL_Vec3f(start.x + 20*(i+1), prevY, 0.0f));
		}
		
	}
	m_points.push_back(end);

	std::sort(m_points.begin(), m_points.end(), SortXValues);
	return positions;
}

float Terrain::GetRandomXValue()
{
	return rand() % SCREENWIDTH;
}

float Terrain::GetRandomYValue()
{
	int tmp = rand() % (400 - 200) + 200;
	CL_Console::write_line("%1",tmp);	
	return tmp;
}


void Terrain::Draw(CL_GraphicContext& gc)
{
	
/*	CL_Draw::circle(gc, m_points[0], 5, CL_Colorf::white);
	CL_Draw::circle(gc, m_points[1], 5, CL_Colorf::white);
	CL_Draw::line(gc,m_points[0].x, m_points[0].y, m_points[1].x, m_points[1].y, lightblue);*/

	CL_Vec3f prevPoint = CL_Vec3f(0.0f, 0.0f, 0.0f);

	for (vector<CL_Vec3f>::iterator it=m_points.begin(); it!=m_points.end(); ++it)
	{
		//CL_Draw::circle(gc, *it, 1, CL_Colorf::white);
		
		if(prevPoint.x != 0 || prevPoint.y != 0)
		{
			if(prevPoint.y == it->y)
			{
				CL_Draw::line(gc, prevPoint.x, prevPoint.y, it->x, it->y, CL_Colorf::lightseagreen);
			}
			else
			{
				CL_Draw::line(gc, prevPoint.x, prevPoint.y, it->x, it->y, CL_Colorf::white);
			}
			
		}
		prevPoint = *it;
	}
}